<?php
Class StaticPage{
  function pageBegin(){
    ob_start();
   }
  function pageEnd(){
   // ob_end_flush();
  	ob_end_clean();
  }
  function getPageName($url,$extendName,$dafault='index.php')
  {
	  $baseName=basename($url);
	  $pageName =substr($baseName,0,strpos($baseName,'.php'));
	  if($pageName =='')
	  {
		  $pageName =$dafault;
	  }
	  $searchStr=substr(strchr($baseName,'?'),1,strlen(strchr($baseName,'?')));
	  $arraySearchStr = explode('&',$searchStr);
	  $searchName ='';
	  for($i=0;$i<count($arraySearchStr);$i++)
	  {
		  $temp=strstr($arraySearchStr[$i],'=');
		  if($temp!='')
		  {
			  $searchName.='-'.substr($temp,1,strlen($temp));
		  }
	  }
	  $fileName = $pageName.$searchName.'.'.$extendName;
	  return $fileName;
    }
	
	function makeStaticPage($url,$extendName,$saveDir,$fileName='')
	{
		
		$this->pageBegin();
		
		$content=@file_get_contents($url);
		
		echo $content;
		//include($url);
		$content= ob_get_contents();
		$this->pageEnd();
	
		if(!is_dir($saveDir))
		{
			
			mkdir($saveDir);
			
		}
		if($fileName=='')
		{
		$fileName=$this->getPageName(basename($url),$extendName);
		}
		else 
		{
			$fileName=$fileName.'.'.$extendName;
		}
		$fp=fopen($saveDir.'/'.$fileName,'w');
		 fwrite($fp,$content);
		
		
	}
	function reMakeCondition($saveDirAndFile,$reMakeTime = -1)
	{
		if($reMakeTime == -1)
		{
			if(file_exist($saveDirAndFile))
			{
				return true;
			}
		}
		elseif(file_exists($saveDirAndFile)&&time()-filemtime($saveDirAndFile)>$reMakeTime)
		{
			return true;
		}
		
		return false;
	}
}